package com.graduate.controller;

import com.graduate.exception.BizException;
import com.graduate.utils.BaseServiceUtils;
import com.graduate.utils.RespBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 基础调用Controller
 *
 * @Createtime : 2020-04-01
 * @Creator : jingchu
 */
@RestController
//@Controller(配合ResponseBody使用)
@EnableAutoConfiguration
@RequestMapping("/imformation")
public class BaseController extends BaseServiceUtils{

    //value触发的什么情况时进行重试,maxAttempts最大重试次数,delay延迟时间,multiplier上次延迟时间是这次的倍数
    @Retryable(value = BizException.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    @RequestMapping(value = "/insertMsg", method = RequestMethod.POST)
    @Transactional
    public RespBody insertMsg(@RequestBody Map<String, Object> requesetMap) {

        return RespBody.success(insert(requesetMap));
    }

    @Retryable(value = BizException.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    @RequestMapping(value = "/updateMsg", method = RequestMethod.PUT)
    @Transactional
    public RespBody updateMsg(@RequestParam("id") String id, @RequestBody Map<String, String> requestMap) {
        return RespBody.success(update(id,requestMap));
    }


    @Retryable(value = BizException.class, maxAttempts = 3, backoff = @Backoff(delay = 2000, multiplier = 1.5))
    @RequestMapping(value = "/deleteMsg", method = RequestMethod.DELETE)
    @Transactional
    public RespBody deleteMsg(@RequestParam("id") String id, @RequestBody Map<String, String> requestMap) {
        return RespBody.success(delete(id,requestMap));
    }

    @RequestMapping(value = "/selectMsgList", method = RequestMethod.GET)
    public RespBody selectMsgList(@RequestBody Map<String, Object> requesetMap) {
        //RouterListUtils routerListUtils = new RouterListUtils();
        return RespBody.success(selectList(requesetMap));
    }

    @RequestMapping(value = "/selectMsg", method = RequestMethod.GET)
    public RespBody selectMsg(@RequestParam("id") String id, @RequestParam("table") String table, @RequestParam("fieldsMsg") String fieldsMsg) {
        return RespBody.success(select(id,table,fieldsMsg));
    }

}
